/*Version 1.0*/
#include "Boite_saisie_animation.h"
#include <string.h>
#include <stdlib.h>

/*Fonction qui appele les differents champs*/
/*pour saisir les parametres des animations*/
void boite_saisie_animation(Marqueur * mq,Scenario * sc)
{
  champ_marqueur * cm=malloc(sizeof(champ_marqueur));
  Sc * S=malloc(sizeof(Sc));

  /*Declaration de la fenetre et de la boite*/
  GtkWidget *pWindow,*pVBox;
  
  /*Champs de saisie des parametres*/
  GtkWidget * champs_duree; 
  GtkWidget * champs_nom_marqueur; 
  GtkWidget * champs_position_scenario;
  GtkWidget * champs_position_tableau;
  GtkWidget * champs_vitesse;
  GtkWidget * champs_acceleration;

  GtkWidget *Button_ok,*Button_ok_scenario;
  GtkWidget *pLabel,*pLabel2,*pLabel3,*pLabel4,*pLabel5,*pLabel6;

  /*Affichage de la fenetre*/
  pWindow = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title(GTK_WINDOW(pWindow), "Parametrage animation");
  gtk_window_set_default_size(GTK_WINDOW(pWindow), 150, 100);
  pVBox = gtk_box_new(TRUE, 0);
  gtk_container_add(GTK_CONTAINER(pWindow), pVBox);

  /*** Declaration des champs de saisie *********/

  pLabel = gtk_label_new("Duree animation : milliseconde");
  pLabel2 = gtk_label_new("Nom Marqueur");
  pLabel3 = gtk_label_new("Position scenario : milliseconde");
  pLabel4 = gtk_label_new("Position tableau : un entier");
  pLabel5 = gtk_label_new("Vitesse : un double");
  pLabel6 = gtk_label_new("Acceleration : un double");

  champs_duree = gtk_entry_new();
  champs_nom_marqueur = gtk_entry_new();
  champs_position_scenario = gtk_entry_new();
  champs_position_tableau = gtk_entry_new();
  champs_vitesse = gtk_entry_new();
  champs_acceleration = gtk_entry_new();

  /*Inclusion des champs de saisie dans la boite*/
  gtk_box_pack_start(GTK_BOX(pVBox),pLabel, TRUE, FALSE, 0);
  gtk_box_pack_start(GTK_BOX(pVBox),champs_duree, TRUE, FALSE, 0);
  
  gtk_box_pack_start(GTK_BOX(pVBox),pLabel2, TRUE, FALSE, 0);
  gtk_box_pack_start(GTK_BOX(pVBox),champs_nom_marqueur, TRUE, FALSE, 0);
  
  gtk_box_pack_start(GTK_BOX(pVBox),pLabel3, TRUE, FALSE, 0);
  gtk_box_pack_start(GTK_BOX(pVBox),champs_position_scenario, TRUE, FALSE, 0);

  gtk_box_pack_start(GTK_BOX(pVBox),pLabel4, TRUE, FALSE, 0);
  gtk_box_pack_start(GTK_BOX(pVBox),champs_position_tableau, TRUE, FALSE, 0);

  gtk_box_pack_start(GTK_BOX(pVBox),pLabel5, TRUE, FALSE, 0);
  gtk_box_pack_start(GTK_BOX(pVBox),champs_vitesse, TRUE, FALSE, 0);

  gtk_box_pack_start(GTK_BOX(pVBox),pLabel6, TRUE, FALSE, 0);
  gtk_box_pack_start(GTK_BOX(pVBox),champs_acceleration, TRUE, FALSE, 0);

  Button_ok = gtk_button_new_with_label("Remplir marqueur");
  gtk_box_pack_start(GTK_BOX(pVBox),Button_ok, TRUE, FALSE, 0);

  Button_ok_scenario = gtk_button_new_with_label("Remplir scenario");
  gtk_box_pack_start(GTK_BOX(pVBox),Button_ok_scenario, TRUE, FALSE, 0);

  cm->m = mq;
  cm->Box = pVBox;

  /*Appel de la fonction qui remplit la structure marqueur*/
  g_signal_connect(G_OBJECT(Button_ok),"clicked",G_CALLBACK(RemplirStructureMarqueur),(champ_marqueur*)cm);
  
  S->m = cm->m;
  S->s = sc;

  /*Appel de la fonction qui remplit la structure scenario*/
  g_signal_connect(G_OBJECT(Button_ok_scenario),"clicked",G_CALLBACK(RajouterMarqueur),(Sc*)S);

  gtk_widget_show_all(pWindow);
}

/*Fonction qui remplit la structure marqueur qui contient les*/
/*les parametres de l'animation*/
void RemplirStructureMarqueur(GtkWidget *pButton,gpointer data)
{
  GtkWidget *champs;
  GList *list;/*liste doublement chainee*/
  const gchar *texte_champs;
  
  Marqueur * mq;
  champ_marqueur * cm=(champ_marqueur*)data;
  mq=cm->m;

  /* Recuperation de la liste des elements que contient la GtkVBox */
  list = gtk_container_get_children(GTK_CONTAINER((GtkWidget*)cm->Box));
  
  list = g_list_next(list);
  /*On recupere le champ duree*/
  champs = GTK_WIDGET(list->data);
  /*Recuperation du texte contenu dans le GtkEntry*/
  texte_champs = gtk_entry_get_text(GTK_ENTRY(champs));
  /*On convertit la chaine en int*/
  mq->duree=atoi(texte_champs);
  
  /*On continue pour les autres champs*/
  list = g_list_next(list);
  list = g_list_next(list);
  champs = GTK_WIDGET(list->data);
  texte_champs = gtk_entry_get_text(GTK_ENTRY(champs));
  mq->nom_marqueur = (gchar*)texte_champs;
  
  list = g_list_next(list);
  list = g_list_next(list);
  champs = GTK_WIDGET(list->data);
  texte_champs = gtk_entry_get_text(GTK_ENTRY(champs));
  mq->position_scenario = atoi(texte_champs);

  list = g_list_next(list);
  list = g_list_next(list);
  champs = GTK_WIDGET(list->data);
  texte_champs = gtk_entry_get_text(GTK_ENTRY(champs));
  mq->position_tableau = atoi(texte_champs);

  list = g_list_next(list);
  list = g_list_next(list);
  champs = GTK_WIDGET(list->data);
  texte_champs = gtk_entry_get_text(GTK_ENTRY(champs));
  mq->vitesse=atof(texte_champs);

  list = g_list_next(list);
  list = g_list_next(list);
  champs = GTK_WIDGET(list->data);
  texte_champs = gtk_entry_get_text(GTK_ENTRY(champs));
  mq->acceleration=atof(texte_champs);
 
  g_list_free(list);
}
